<link href="/css/formSelects-v4.css" rel="stylesheet" />

<div class="layuimini-main">

    <form class="layui-form layuimini-form" lay-filter="editTriggerForm">
        <div class="layui-form-item" hidden>
            <label class="layui-form-label required">编号</label>
            <div class="layui-input-block">
                <input type="number" name="id" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">接料口</label>
            <div class="layui-input-block">
                <select id="sourceportselect" name="sourceId">
                    <option value="">请选择接料口</option>
                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label required">组合条件</label>
            <div class="layui-input-block">
                <input type="radio" name="link" value="Single" title="Sinle">
                <input type="radio" name="link" value="And" title="And">
                <input type="radio" name="link" value="Any" title="Any">

            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">送料口</label>
            <div class="layui-input-block">
                <select id="targetportselect" name="targetIds" xm-select="select1">
                    <option value="">请选择送料口</option>
                </select>
            </div>
        </div>



        <div class="layui-form-item">
            <label class="layui-form-label required">优先级</label>
            <div class="layui-input-block">
                <input type="number" name="priority" placeholder="请输入任务优先级" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">指定机器人</label>
            <div class="layui-input-block">
                <select id="robots" name="assignRobot" lay-filter="deviceType">
                    <option value="0">请选择机器人</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'table', 'formSelects'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            formSelects = layui.formSelects,
            $ = layui.$;

        form.verify({
            ip: [
                /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,
                'IP地址不符合规则'
            ],

        });

        // 初始化表单
        form.render();

        //动态加载 设备列表
        $.ajax({
            type: 'get',
            async: false,
            url: 'api/ports/GetAll',
            dataType: 'json',
            success: function (res) {

                var keys = [];
                $.each(res.data, function (index, item) {
                    var temp = {
                        "name": item.portName,
                        "value": item.id
                    };
                    keys.push(temp)

                });
                formSelects.data("select1", "local", {
                    arr: keys
                });

                $.each(res.data, function (index, item) {
                    //通过LayUI.jQuery添加列表项
                    $("#sourceportselect").append(new Option(item.portName, item.id));
                });

                form.render('select');
            }
        });

        //动态加载 机器人
        $.ajax({
            type: 'get',
            async: false,
            url: 'api/Robot/GetAllRobots',
            dataType: 'json',
            success: function (res) {
                $.each(res.data, function (index, item) {
                    $("#robots").append(new Option(item.ip, item.ip));

                });

                form.render('select');
            }
        });
   
        //动态加载 设备列表
        $.ajax({
            type: 'get',
            async: false,
            url: '/Port/GetAll',
            dataType: 'json',
            success: function (res) {
                var keys = [];
                $.each(res.data, function (index, item) {
                    var temp = {
                        "name": item.portName,
                        "value": item.id
                    };
                    keys.push(temp)
                });
                formSelects.data("select1", "local", {
                    arr: keys
                });



            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                url: 'api/Trigger/Update/' + data.field.id,
                method: 'put',
                contentType: 'application/json',
                cache: false,
                data: JSON.stringify(data.field),
                success: function (res) {
                    if (res.code === 200) {
                        var index = layer.msg('修改成功!', { icon: 1 }, function () {
                            {
                                layer.closeAll();
                            }
                        });
                    }
                    else
                        layer.msg('修改失败!', { icon: 2 });
                },
                error: function (data) {
                    layer.msg('修改出错!', { icon: 2 });

                }
            });

            return false;
        });


    });</script>